package com.googlecode.animator.controller
{
	
	import com.googlecode.animator.data.IValueObject;
	import com.googlecode.animator.data.cameras.ActualViewCameraVO;
	import com.googlecode.animator.data.objects3d.CameraProxyVO;
	import com.googlecode.animator.data.objects3d.Object3DVO;
	import com.googlecode.animator.data.objects3d.TargetProxyVO;
	import com.googlecode.animator.events.PropertiesPanelEvent;
	import com.googlecode.animator.view.properties.types.Depth;
	import com.googlecode.animator.view.properties.types.Height;
	import com.googlecode.animator.view.properties.types.Object3DName;
	import com.googlecode.animator.view.properties.types.Width;
	import com.googlecode.animator.view.properties.types.Xpos;
	import com.googlecode.animator.view.properties.types.Ypos;
	import com.googlecode.animator.view.properties.types.Zpos;
	
	import org.papervision3d.core.math.Number3D;
	import org.puremvc.as3.interfaces.INotification;
	
	/**
	 * Handles a change to an Object3D's properties. 
	 * @author USER
	 * 
	 */
	public class PropertiesUpdatePropertyCommand extends BaseCommand
	{
		/**
		 * Executes command. 
		 * @param notification
		 * 
		 */
		override public function execute(notification:INotification):void
		{
			super.execute(notification);
			
			var propEvent:PropertiesPanelEvent = notification.getBody() as PropertiesPanelEvent;
			var VO:Object = propEvent.VO;
			
			var pos:Number3D = settings.getPosObject3DInScene(VO.name);

			if(propEvent.target is Object3DName)
			{
				settings.setObject3DLabel(VO.name, VO.label);
			}
			
			if(propEvent.target is Xpos)
			{				
				pos.x = VO.x;
				settings.setPosObject3D(VO.name, pos);
				cameraCheck(VO);

			}
			
			if(propEvent.target is Ypos)
			{
				pos.y = VO.y;
				settings.setPosObject3D(VO.name, pos);
				cameraCheck(VO);
			}
			
			if(propEvent.target is Zpos)
			{
				pos.z = VO.z;
				settings.setPosObject3D(VO.name, pos);
				cameraCheck(VO);
			}			
			
			if(propEvent.target is Width)
			{
				//settings.setObject3DHeight(name, VO.height);
				settings.setObject3DDimension(VO.name, "width", VO.width);
			}
			
			if(propEvent.target is Height)
			{
				settings.setObject3DDimension(VO.name, "height", VO.height);
			}
			
			if(propEvent.target is Depth)
			{
				settings.setObject3DDimension(VO.name, "depth", VO.depth);
			}
			
		}
		
		protected function cameraCheck(obj:Object):void
		{
			var VO:Object3DVO = settings.getObject3D(obj.name);
			
			if(object3D is CameraProxyVO || object3D is TargetProxyVO)
			{
				var cameraPos:Number3D = new Number3D();
				var targetPos:Number3D = new Number3D();
				
				for each(var object3D:Object3DVO in settings.objects3D)
				{
					if(object3D is CameraProxyVO )
					{
						cameraPos.x = object3D.x;
						cameraPos.y = object3D.y;
						cameraPos.z = object3D.z;			
					}
					
					if(object3D is TargetProxyVO )
					{
						targetPos.x = object3D.x;
						targetPos.y = object3D.y;
						targetPos.z = object3D.z;
					}
					
				}
				
				for each(var camera:IValueObject in settings.cameras)
				{
					if(camera as ActualViewCameraVO)
					{
						
						settings.setPosCamera((camera as ActualViewCameraVO).name, cameraPos, targetPos); 
					}
				}
			}
		}
	}
}